﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.LevelConfig
{
    public class LevelController
    {
        public static DataTable GetConfig()
        {
            LevelFilter filter = new LevelFilter();
            return filter.GetConfigPercent();
        }

        public static DataTable GetConfigIDByPercent(decimal percet)
        {
            LevelFilter filter = new LevelFilter();
            filter.Percent = percet;
            return filter.GetConfigPercent();
        }

        public static Level GetConfigByID(int ID)
        {
            LevelFilter filter = new LevelFilter();
            filter.Level1 = ID;
            DataTable dt = filter.GetConfigPercent();
            if (ValidationHelper.HasRow(dt))
            {
                return new Level(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public static bool Insert(Level lvt)
        {
            return ExecuteStoredProcedure(lvt, StoreProceduAction.AddNew.ToString());
        }

        public static bool Delete(Level lvt)
        {
            return ExecuteStoredProcedure(lvt, StoreProceduAction.Delete.ToString());
        }

        public static bool UpdateValue(Level lvt, StoreProceduAction action)
        {
            return ExecuteStoredProcedure(lvt, action.ToString());
        }

        private static bool ExecuteStoredProcedure(Level lvt, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "pr_LevelConfig";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@ID", lvt.Id);
            sqlHelper.AddParameter("@LevelInfo", lvt.Level1);
            sqlHelper.AddParameter("@NumOfPeople", lvt.NumOfPeople);
            sqlHelper.AddParameter("@percent", lvt.Percent);
            return sqlHelper.ExecuteSQL(sProc);
        }
    }
}
